home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / QT MovieToolBox / other QT code not used yet / QuickDraw Movies Library.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-25  |  5.2 KB  |  236 lines  |  [TEXT/KAHL]

  1. /*
  2.     File:        QuickDraw Movies Library.c
  3.  
  4.     Contains:    Some routines simplify basic movie construction. 
  5.  
  6.     Written by:    xxx put writers here xxx
  7.  
  8.     Copyright:    © 1991 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <1+>    11/12/91    dvb        Bring into the present, November 1991.
  13.          <1>      8/7/91    dvb        first checked in
  14.  
  15.     To Do:
  16. */
  17.  
  18. /*
  19.     File:        Quickdraw Movies Library.c
  20.  
  21. */
  22.  
  23. /*
  24.  * file: Graphics Movies Library.c
  25.  *
  26.  * started 15 December 1990 13:39:32 Saturday
  27.  * david van brink
  28.  *
  29.  * Some routines to make it a snap! to make quickdraw movies.
  30.  *
  31.  */
  32.  
  33.  
  34. #define noFrameDiffx
  35.  
  36. #include <QuickDraw.h>
  37. #include <QDOffscreen.h>
  38. #include <Memory.h>
  39.  
  40. #include <Movies.h>
  41. #include <MoviesFormat.h>
  42. #include <ImageCompression.h>
  43.  
  44. #include "QuickDraw Movies Library.h"
  45.  
  46. /*-----------------------------
  47.     Constants
  48. -----------------------------*/
  49.  
  50. #define kDefaultTemporalQuality 3L
  51. #define kDefaultKeyFrameRate 20
  52.  
  53. #define kFixedPointUnity 0x00010000
  54.  
  55. /*-----------------------------
  56.     Prototypes
  57. -----------------------------*/
  58. static void FailNil(long x);
  59. #define FailNil(x) FailNil((long)(x))
  60.  
  61. /*-----------------------------
  62.     Routines
  63. -----------------------------*/
  64.  
  65. void InitializeMovieRecorder(MovieRecorder *mr,GWorldPtr gw,unsigned char *fileName,long frameRate,
  66.         long compressionType,long compressionQuality)
  67.     {
  68.     short thisError;
  69.     long compressedFrameSize;
  70.  
  71.     mr->compressionType = compressionType;
  72.     mr->compressionQuality = compressionQuality;
  73.     mr->temporalQuality = kDefaultTemporalQuality<<8;
  74.     mr->keyFrameRate = kDefaultKeyFrameRate;
  75.     mr->gWorldPtr = gw;
  76.     mr->pixMapH = gw->portPixMap;
  77.  
  78. //    if(mr->compressionType == 'rpza')
  79.     //    mr->destDepth = 16;
  80.     //else
  81.     //    mr->destDepth = (**mr->pixMapH).pixelSize;
  82.  
  83.     mr->time = 0;
  84.  
  85.     mr->imageDescriptionH = (ImageDescription **)NewHandle(0);
  86.  
  87.     /*
  88.      * Assume we record from
  89.      *  the entire viewDevice.
  90.      */
  91.  
  92.     mr->sourceRect = (**mr->pixMapH).bounds;
  93.  
  94.     /*
  95.      * Create the file, and the track, and the media.
  96.      */
  97.  
  98.     FSMakeFSSpec(0,0,fileName,&mr->movieDataSpec);
  99.  
  100.     thisError = CreateMovieFile( &mr->movieDataSpec, 'TVOD', 0,createMovieFileDeleteCurFile,
  101.             &mr->resourceRefNum,&mr->movie);        /* deletes old one */
  102.  
  103.     mr->track = NewMovieTrack(mr->movie,
  104.         (long)( mr->sourceRect.right - mr->sourceRect.left)<<16,
  105.         (long)(mr->sourceRect.bottom - mr->sourceRect.top)<<16,
  106.         0);
  107.  
  108.     mr->media = NewTrackMedia(mr->track, VIDEO_TYPE,frameRate, 0,0);
  109.  
  110.     BeginMediaEdits(mr->media);
  111.  
  112.     /*
  113.      * Find out how much buffer to allocate
  114.      * for compression, and allocate it.
  115.      */
  116.  
  117.  
  118.     thisError = GetMaxCompressionSize(mr->pixMapH,&mr->sourceRect,
  119.             0,
  120.             mr->compressionQuality,
  121.             mr->compressionType,0,&compressedFrameSize);
  122.     if(thisError)    Debugger();
  123.  
  124. #ifndef noFrameDiff
  125.     thisError = CompressSequenceBegin(&mr->sequenceID,
  126.             mr->pixMapH,0,    /*mr->previousPixMapH,*/
  127.             &mr->sourceRect,nil,
  128.             0,
  129.             mr->compressionType,0,mr->compressionQuality,
  130.             mr->temporalQuality,mr->keyFrameRate,nil,
  131.             codecFlagUpdatePrevious,mr->imageDescriptionH);
  132. #endif
  133.  
  134.     if(thisError) Debugger();
  135.  
  136.     mr->compressedFrameBitsH = NewHandle(compressedFrameSize);
  137.     FailNil(mr->compressedFrameBitsH);
  138.     }
  139.  
  140. void AddMovieRecorderFrame(mr)
  141. /*
  142.   * Record the next frame out of the
  143.   * MovieRecorder's viewDevice
  144.   */
  145.     register MovieRecorder *mr;
  146. {
  147.     long compressedFrameSize;
  148.     short thisError;
  149.     ImageDescription **idH;
  150.     unsigned char similarity;
  151.  
  152.     idH = (ImageDescription **)NewHandle(0);
  153.  
  154.     HLock(mr->compressedFrameBitsH);
  155.  
  156. #ifdef noFrameDiff
  157.     thisError = CompressImage(mr->pixMapH,&mr->sourceRect, 
  158.             mr->compressionQuality,mr->compressionType,
  159.             idH,*mr->compressedFrameBitsH);
  160. #else
  161.  
  162.     thisError = CompressSequenceFrame(mr->sequenceID,mr->pixMapH,&mr->sourceRect,
  163.             codecFlagUpdatePrevious,*mr->compressedFrameBitsH,&compressedFrameSize, &similarity,nil);
  164.  
  165.     (**mr->imageDescriptionH).dataSize = 0;
  166. #endif
  167.     if(thisError) Debugger();
  168.  
  169.  
  170.     HUnlock(mr->compressedFrameBitsH);
  171.  
  172. #ifdef noFrameDiff
  173.     thisError = AddMediaSample(mr->media,mr->compressedFrameBitsH,
  174.             0,(**idH).dataSize,1,(SampleDescriptionHandle)idH, 1,0,&mr->time);
  175. #else
  176.     thisError = AddMediaSample(mr->media,mr->compressedFrameBitsH,
  177.             0,compressedFrameSize,1,
  178.             (SampleDescriptionHandle)mr->imageDescriptionH,
  179.             1,similarity ? mediaSampleNotSync : 0,&mr->time);
  180. #endif
  181.     if(thisError)    Debugger();
  182.  
  183.     mr->time++;
  184.  
  185.     DisposHandle((Handle)idH);
  186.     }
  187.  
  188.  
  189. void TerminateMovieRecorder(mr)
  190.     MovieRecorder *mr;
  191. {
  192.     short thisError;
  193.     short resRefNum;
  194.     TimeValue mediaDuration,trackDuration,movieDuration;
  195.  
  196. #ifdef noFrameDiff
  197. #else
  198.     CDSequenceEnd(mr->sequenceID);
  199. #endif
  200.  
  201.     EndMediaEdits(mr->media);
  202.  
  203.     mediaDuration = GetMediaDuration(mr->media);
  204.  
  205.     thisError = InsertMediaIntoTrack(mr->track,0,0,mediaDuration,kFixedPointUnity);
  206.  
  207.     trackDuration = GetTrackDuration(mr->track);
  208.     movieDuration = GetMovieDuration(mr->movie);
  209.  
  210.     thisError = OpenMovieFile(&mr->movieDataSpec, &resRefNum, fsRdWrPerm);
  211.     thisError = AddMovieResource( mr->movie, resRefNum, 0, 0);
  212.     thisError = CloseMovieFile(resRefNum);
  213.  
  214.     DisposeHandle(mr->compressedFrameBitsH);
  215.  
  216.     SetMoviePreferredRate( mr->movie, kFixedPointUnity );
  217.     thisError = GetMoviesError();
  218.  
  219.     DisposeMovie(mr->movie);
  220.     thisError = GetMoviesError();
  221.  
  222.     }
  223.  
  224.  
  225.  
  226. #undef FailNil
  227. static void FailNil(x)
  228.     long x;
  229.     {
  230.     if(!x)
  231.         {
  232.         DebugStr((unsigned char *)"\pOut of memory.");
  233.         ExitToShell();
  234.         }
  235.     }
  236.